package org.rcy.ruicingmarket.category;

import org.rcy.framework.api.distributed.DistributedTransaction;
import org.rcy.framework.api.distributed.DistributedTransactional;
import org.rcy.framework.api.entity.PageResult;
import org.rcy.framework.cache.redis.CommonCache;
import org.rcy.ruicingmarket.goods.GoodsPageQueryRequest;
import org.rcy.ruicingmarket.exception.GoodsException;
import org.rcy.ruicingmarket.common.utils.CollectionBeanUtils;
import org.rcy.ruicingmarket.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/12/20 11:47
 */

@Service
public class CategoryService {

	@Autowired
	private CategoryDao categoryDao;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private CommonCache commonCache;

	public PageResult<CategoryVo> pageQuery(CategoryRequest request){
		PageResult<CategoryVo> pageResult = new PageResult<>();
		int count = categoryDao.countCondition(request);
		if(count < 1){
			return pageResult;
		}
		List<Category> categories = categoryDao.pageQuery(request);
		pageResult.setPageNo(request.getPageNo());
		pageResult.setPageSize(request.getPageSize());
		pageResult.setRecordTotal(count);
		pageResult.setResult(CollectionBeanUtils.copyListProperties(categories,CategoryVo::new));
		return pageResult;
	}

	public List<CategoryVo> getAllLevelOneCategory(){
		List<Category> levelOneCategory = categoryDao.getAllLevelOneCategory();
		return CollectionBeanUtils.copyListProperties(levelOneCategory,CategoryVo::new);
	}

	public CategoryVo findById(Long id){
		commonCache.get("sdad");
		Category category = categoryDao.get(id);
		if(category == null) {
			return null;
		}
		List<Category> children = categoryDao.findByPid(id);
		category.setChildren(children);
		return convertToVo(category);
	}

	public Long addCategory(CategoryVo vo){
		Category save = categoryDao.save(convertToPo(vo));
		return save.getId();
	}

	public void deleteCategory(Long id){
		int dependenedCount = 0;
		Category category = categoryDao.get(id);
		List<Category> children = categoryDao.findByPid(id);
		GoodsPageQueryRequest condition = new GoodsPageQueryRequest();
		if(children != null) {
			for (Category child : children) {
				condition.setCid(child.getId());
				dependenedCount += goodsService.countByCondition(condition);
			}
		}
		condition.setCid(category.getId());
		dependenedCount += goodsService.countByCondition(condition);
		if(dependenedCount > 0 ){
			throw new GoodsException("该分类仍被商品依赖，无法进行删除");
		}
		if(children != null) {
			for (Category child : children) {
				categoryDao.delete(child.getId());
			}
		}
		categoryDao.delete(id);
	}

	private CategoryVo convertToVo(Category category){
		CategoryVo categoryVo = new CategoryVo();
		categoryVo.setId(category.getId());
		categoryVo.setName(category.getName());
		categoryVo.setDescription(category.getDescription());
		categoryVo.setCreateTime(category.getCreateTime());
		categoryVo.setPid(category.getPid());
		if(category.getChildren() != null) {
			categoryVo.setChildren(CollectionBeanUtils.copyListProperties(category.getChildren(),CategoryVo::new));
		}
		return categoryVo;
	}

	private Category convertToPo(CategoryVo vo) {
		Category category = new Category();
		category.setId(vo.getId());
		category.setName(vo.getName());
		category.setDescription(vo.getDescription());
		category.setPid(vo.getPid());
		return category;
	}

	public DistributedTransaction testDx(CategoryVo vo,DistributedTransaction transaction) {
		try {
			Category category = new Category();
			category.setName("测试时");
			categoryDao.insertSelective(category);
		}catch (Exception e){
			System.out.println(e.getMessage());
			transaction.setRollback(true);
			transaction.setException(e);
		}
		return transaction;
	}
}
